package com.scpii.api.domain.auth;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.scpii.api.common.Constants;
import com.scpii.api.common.base.FilterEntity;
import com.scpii.api.common.json.serializer.ResourceUrlSerializer;

public class UsersInfoDetails extends FilterEntity implements UserDetails,
		Serializable {

	private Long id;

	private String userName;

	private String userAilas;

	@JsonIgnore
	private String password;

	private Date createDt;

	@JsonIgnore
	private Long createBy;

	private Date updateDt;

	private Long updateBy;

	@JsonIgnore
	private String authoritiesStr;

	private String userType;

	@JsonIgnore
	private String passwordFormat;

	private Long appId;

	@JsonSerialize(using = ResourceUrlSerializer.class, include = JsonSerialize.Inclusion.NON_NULL)
	private String avatar;

	@JsonIgnore
	private String validCode;

	private String provider;

	private Integer rankPoint;

	private Long rankId;

	private Long addressId;

	@JsonIgnore
	private Long deviceId;

	private String phoneNumber;

	private String email;

	private Date lastLoginDt;

	private Date lastActivityDt;

	private static final long serialVersionUID = 1L;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName == null ? null : userName.trim();
	}

	public String getUserAilas() {
		return userAilas;
	}

	public void setUserAilas(String userAilas) {
		this.userAilas = userAilas == null ? null : userAilas.trim();
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password == null ? null : password.trim();
	}

	public Date getCreateDt() {
		return createDt;
	}

	public void setCreateDt(Date createDt) {
		this.createDt = createDt;
	}

	public Long getCreateBy() {
		return createBy;
	}

	public void setCreateBy(Long createBy) {
		this.createBy = createBy;
	}

	public Date getUpdateDt() {
		return updateDt;
	}

	public void setUpdateDt(Date updateDt) {
		this.updateDt = updateDt;
	}

	public Long getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(Long updateBy) {
		this.updateBy = updateBy;
	}

	public String getAuthoritiesStr() {
		return authoritiesStr;
	}

	public void setAuthoritiesStr(String authoritiesStr) {
		this.authoritiesStr = authoritiesStr;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType == null ? null : userType.trim();
	}

	public String getPasswordFormat() {
		return passwordFormat;
	}

	public void setPasswordFormat(String passwordFormat) {
		this.passwordFormat = passwordFormat == null ? null : passwordFormat
				.trim();
	}

	public Long getAppId() {
		return appId;
	}

	public void setAppId(Long appId) {
		this.appId = appId;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar == null ? null : avatar.trim();
	}

	public String getValidCode() {
		return validCode;
	}

	public void setValidCode(String validCode) {
		this.validCode = validCode == null ? null : validCode.trim();
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider == null ? null : provider.trim();
	}

	public Integer getRankPoint() {
		return rankPoint;
	}

	public void setRankPoint(Integer rankPoint) {
		this.rankPoint = rankPoint;
	}

	public Long getRankId() {
		return rankId;
	}

	public void setRankId(Long rankId) {
		this.rankId = rankId;
	}

	public Long getAddressId() {
		return addressId;
	}

	public void setAddressId(Long addressId) {
		this.addressId = addressId;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber == null ? null : phoneNumber.trim();
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email == null ? null : email.trim();
	}

	public Date getLastLoginDt() {
		return lastLoginDt;
	}

	public void setLastLoginDt(Date lastLoginDt) {
		this.lastLoginDt = lastLoginDt;
	}

	public Date getLastActivityDt() {
		return lastActivityDt;
	}

	public void setLastActivityDt(Date lastActivityDt) {
		this.lastActivityDt = lastActivityDt;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}

	@JsonIgnore
	@Override
	public String getUsername() {
		return userName;
	}

	@JsonIgnore
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@JsonIgnore
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@JsonIgnore
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@JsonIgnore
	@Override
	public boolean isEnabled() {
		if (ArrayUtils
				.contains(Constants.USERS_NON_ENABLE_TYPES_ARRY, userType)) {
			return false;
		}
		return true;
	}

	@JsonIgnore
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (authoritiesStr == null) {
			return null;
		}
		String[] authoritiesInfos = StringUtils.split(authoritiesStr, ",");
		Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
		for (String authoritiesInfo : authoritiesInfos) {
			grantedAuthorities.add(new SimpleGrantedAuthority(authoritiesInfo));
		}
		return grantedAuthorities;
	}

}